import Vue from "vue";
import VueRouter from "vue-router";
import Router from "vue-router";
Vue.use(Router)

// const routes = [
//     { path: '/', redirect: '/home' },
//     { path: '/home', component: () => import('@views/Home') },
//     { path: '/login', component: () => import('@views/Login') },
//     { path: '/user', component: () => import('@views/User') },
//     { path: '/massage', component: () => import('@views/Massage') },
//     { path: '/mymassage', component: () => import('@views/Mymassage') },
// ]


const routes = [
    { path: '/', redirect: '/layout' },
    {
        path: '/layout',
        component: () => import('@/views/Layout.vue'),
        children: [
            {
                path: '',
                component: () => import('@/views/Home.vue'),
                meta: {
                    title: "首页",
                    icon: 'el-icon-platform-eleme'
                },
                name: 'Home'
            },
            {
                path: 'user',
                component: () => import('@/views/User.vue'),
                meta: {
                    title: "用户管理",
                    icon: 'el-icon-user-solid'
                },
                name: 'User'
            },
            {
                path: 'info',
                component: () => import('@/views/Massage.vue'),
                meta: {
                    title: "信息管理",
                    icon: 'el-icon-star-on'
                },
                name: 'Info',
                children: [
                    {
                        path: '',
                        component: () => import('@/views/Mymassage.vue'),
                        meta: {
                            title: "个人信息",
                            icon: 'el-icon-question'
                        },
                        name: 'Myinfo',
                    }
                ]
            }
        ]
    }, //首页
    { path: '/login', component: () => import('@/views/Login.vue') }, // 登录页

]

const router = new VueRouter({
    mode: "history",
    routes
})

import store from "../store";
import { pressApi } from "../utile/https";
import { getCookie } from '../utile/cookie'
//设置路由守卫

router.beforeEach((to, from, next) => {
    let token = getCookie('token')

    if (token) {
        // 已经登陆了
        if (to.path == '/login') {
            // 如果登录了手动输入url登录页,则直接跳转到首页
            next('/')
        } else {
            let role = localStorage.getItem('role')
            // console.log(role);
            // 判断禁止无限添加权限数组
            if (store.state.routes.length == 0) {
                // 请求左侧导航栏的数据
                pressApi({ role: role }).then((res) => {
                    res.data.forEach(item => {
                        item.component = () => import(`@/views/press/${item.name}.vue`)
                        routes[1].children.push(item)
                    })
                    // 再次重新添加routes
                    router.addRoutes(routes)
                    store.dispatch('setRoutesAsync', routes)
                    // 禁止刷新出现白屏
                    next({ ...to, replace: true })
                })
            } else {
                next()
            }
        }


    } else if (!token && to.path != '/login') {
        next('/login')
    } else {
        next()
    }
})


// 该段代码不需要记，理解即可
//在vue - router在3.1.0版本之后，push和replace方法会返回一个promise对象，如果跳转到相同的路由，
// 就报promise uncaught异常
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push (location) {
    return originalPush.call(this, location).catch((err) => err);
};



export default router